package com.zj.core.dialog

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import android.widget.SeekBar
import com.fileutils.SPUtils
import com.dialogutils.ToastUtil
import com.zj.core.R
import com.zj.core.base.BaseApplication
import com.zj.core.base.BaseDialog
import com.zj.core.tools.Constant
import com.zj.core.tools.ViewPosition
import com.zj.core.tools.Constant.Companion.TEXT_SIZE_KEY
import com.zj.core.ui.DialogTextSizeUi

/**
 * Created by Administrator on 2019/5/22.
 */

@Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
class TextSizeDialog(context: Activity) : BaseDialog(context),View.OnClickListener{
    private var textDialog: DialogTextSizeUi?=null

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val width = (ViewPosition().getScreenWidth(this.context) * 0.8).toInt()
        val height = (ViewPosition().getScreenHeight(this.context) * 0.5).toInt()
        val params = LinearLayout.LayoutParams(width, height, 0f)

        textDialog= DialogTextSizeUi(context, this, mSeekBar)
        textDialog?.apply {
            setContentView(view_root_dialog, params)
            //setCancelable(cancelable);
            setCanceledOnTouchOutside(true)

            val defultSize = SPUtils.get(BaseApplication.context, Constant.TEXT_SIZE_KEY, 20) as Int
            seek_bar_view?.progress = defultSize
            change_text_size?.text = "" + defultSize
            change_text_size?.textSize = defultSize.toFloat()
        }
    }

    private val mSeekBar = object : SeekBar.OnSeekBarChangeListener {
        @SuppressLint("SetTextI18n")
        override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
            textDialog?.apply {
                change_text_size?.text = "" + progress
                change_text_size?.textSize = progress.toFloat()
            }
        }
        override fun onStartTrackingTouch(seekBar: SeekBar) {}
        override fun onStopTrackingTouch(seekBar: SeekBar) {}
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.common_dialog_cancel_tv->{
                dismiss()
            }

            R.id.common_dialog_confirm_tv->{
                SPUtils.put(BaseApplication.context, TEXT_SIZE_KEY, Integer.parseInt(textDialog?.change_text_size?.text.toString()))
                dismiss()
                ToastUtil.showShort(BaseApplication.context, "修改成功")
            }
        }
    }
}
